package com.testdocs.client.test.testcase.edit;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.common.RichTextToolbar;
import com.testdocs.client.common.dialog.base.DialogPanel;
import com.testdocs.client.test.i18n.TestConstants;
import com.testdocs.client.validation.FieldConstraint;
import com.testdocs.client.validation.ValidationConstraint;

public class EditTestCaseDialogPanel extends Composite implements DialogPanel {
	private static AddTestCaseDialogPanelUiBinder uiBinder = GWT.create(AddTestCaseDialogPanelUiBinder.class);

	@UiTemplate("EditTestCaseDialogPanel.ui.xml")
	interface AddTestCaseDialogPanelUiBinder extends UiBinder<Widget, EditTestCaseDialogPanel> {
	}

	@UiField
	Label nameLabel;
	@UiField
	TextBox nameTextBox;
	@UiField
	Label nameErrorLabel;
	@UiField(provided=true)
	RichTextToolbar toolbar;
	@UiField(provided=true)
	RichTextArea descriptionTextArea;

	private TestConstants testConstants = (TestConstants) GWT.create(TestConstants.class);

	public EditTestCaseDialogPanel() {
		descriptionTextArea = new RichTextArea();
		descriptionTextArea.setSize("100%", "14em");
		toolbar = new RichTextToolbar(descriptionTextArea);
		toolbar.setWidth("100%");
		initWidget(uiBinder.createAndBindUi(this));
		nameLabel.setText(testConstants.testCaseName());
	}
	
	public void setName(String name) {
		nameTextBox.setText(name);
	}
	
	public String getName() {
		return nameTextBox.getText();
	}
	
	public void setDescription(String description) {
		descriptionTextArea.setText(description);
	}
	
	public String getDescription() {
		return descriptionTextArea.getText();
	}
	
	public void clear() {
		nameTextBox.setText("");
		descriptionTextArea.setText("");
		nameErrorLabel.setText("");
	}

	public void handleValidationConstraints(ValidationConstraint[] constraints) {
		for (ValidationConstraint constraint : constraints) {
			if (constraint instanceof FieldConstraint
					&& ((FieldConstraint) constraint).getField().equals(
							"name")) {
				nameErrorLabel.setText(constraint.getMessage());
			}
		}
	}
}
